Popular Searches
Popular Course Categories
Popular Courses

Installing and Configuring the Java Development Kit (JDK)

Installing and Configuring the Java Development Kit (JDK)

Java Installation and Development Environment Setup

Installing and Configuring the Java Development Kit (JDK)

Course: Core Java Training

Topic: Installing and Configuring the Java Development Kit (JDK)

Course Link: Core Java Training Course

Demo Link: Register for a Course Demo

Website: JustAcademy Official Website


1. Introduction to the Java Development Kit (JDK)

The Java Development Kit (JDK) is a software development package required to develop, compile, debug, and run Java applications. It provides the tools and libraries programmers need to create Java programs.

Before writing and executing Core Java programs, a developer should install a suitable JDK and configure the development environment correctly. Without a working Java compiler, Java source code cannot be compiled into bytecode.

The JDK is available for operating systems such as Windows, macOS, and Linux. The installation procedure varies depending on the operating system and the selected JDK distribution.

Why Is the JDK Important?

  • It provides the Java compiler used to compile source code.
  • It includes tools for running and debugging Java applications.
  • It supplies standard Java libraries and development utilities.
  • It allows developers to build projects using command-line tools or IDEs.
  • It provides utilities for inspecting and managing Java applications.

2. Understanding JDK, JRE, and JVM

Before installing Java, it is useful to understand the differences between the JDK, JRE, and JVM.

Component Full Form Purpose
JDK Java Development Kit Provides tools for developing, compiling, debugging, and running Java applications.
JRE Java Runtime Environment Traditionally refers to the runtime components and libraries needed to run Java applications.
JVM Java Virtual Machine Executes Java bytecode and provides the runtime environment for Java programs.

Important: Modern JDK distributions may not provide a separate JRE installer. Installing a JDK is the standard approach when learning Java programming.

3. Prerequisites for Installing the JDK

Check the following requirements before beginning the installation process.

  • A computer running a supported version of Windows, macOS, or Linux.
  • A stable internet connection to download the installer or package.
  • Sufficient storage space for the JDK and development tools.
  • Permission to install software or configure environment variables, where required.
  • Knowledge of the computer's operating system and processor architecture.

Choosing a JDK Version

Choose a JDK version that matches your course requirements, project requirements, and development tools. Long-Term Support (LTS) releases are often suitable for learning and long-running projects. Confirm compatibility before selecting a version.

Official download page: Oracle Java Downloads.

Other distributions are also available, including Eclipse Temurin and Microsoft Build of OpenJDK. Review the applicable licence and support terms before choosing a distribution for commercial use.

4. Downloading the JDK

The first step is to download a JDK package that is compatible with your operating system and hardware.

Step-by-Step Download Process

  1. Open the official JDK download page.
  2. Select the JDK version required for your learning or project.
  3. Choose your operating system: Windows, macOS, or Linux.
  4. Select the appropriate processor architecture, such as x64 or ARM64, when offered.
  5. Download the correct installer or package.
  6. Read the installation instructions and licence terms for the selected distribution.

Use official vendor websites to reduce the risk of downloading modified or unsafe installation files.

5. Installing the JDK on Windows

Windows users can generally install the JDK through a graphical installer or use a supported package-management method. The exact screens and installation paths depend on the distribution and version.

Step 1: Run the Installer

  1. Locate the downloaded JDK installer, usually an executable file with an .exe extension or an MSI package.
  2. Double-click the installer.
  3. Approve the installation prompt if Windows requests permission.
  4. Follow the setup instructions and accept the applicable licence terms.
  5. Choose the installation directory if the installer offers that option.
  6. Complete the installation.

Step 2: Locate the Installation Directory

The JDK may be installed in a directory similar to the following:

C:\Program Files\Java\jdk-25

This is an example path only. The actual directory depends on the installed JDK version and distribution. Locate the folder on your computer before configuring environment variables.

Step 3: Verify the Installation Directory

Open the JDK installation directory and look for a bin folder. It should contain development tools such as java.exe and javac.exe.

The java command launches the Java runtime, while javac compiles Java source files.

6. Configuring the JAVA_HOME Environment Variable

JAVA_HOME is an environment variable that points to the root directory of a JDK installation. Some development tools, build systems, and scripts use this variable to locate Java.

Step-by-Step Configuration on Windows

  1. Open the Windows Start menu and search for Environment Variables.
  2. Select the option for editing the system environment variables.
  3. In the System Properties window, open the Environment Variables dialog.
  4. Under User variables or System variables, select New.
  5. Set the variable name to JAVA_HOME.
  6. Set the variable value to the actual JDK installation directory, such as C:\Program Files\Java\jdk-25.
  7. Save the change.
  8. Ensure the path points to the JDK root folder, not its bin subfolder.

Example

Variable name: JAVA_HOME
Variable value: C:\Program Files\Java\jdk-25

Replace the example path with the actual path on your system. Do not copy it blindly if your installed JDK uses a different directory.

7. Configuring the PATH Environment Variable

The PATH environment variable tells the operating system where to search for executable commands. Adding the JDK's bin directory to PATH allows commands such as java and javac to be used from a terminal without entering their full paths.

Steps to Add JDK to PATH on Windows

  1. Open the Environment Variables dialog.
  2. Find the appropriate Path variable under User variables or System variables.
  3. Select Edit.
  4. Add a new entry that references the JDK's bin directory.
  5. For example, use %JAVA_HOME%\bin.
  6. Save the changes in each dialog.
  7. Close any previously opened Command Prompt or terminal windows.
  8. Open a new terminal and verify the configuration.

Example PATH Entry

%JAVA_HOME%\bin

Best practice: Add a separate PATH entry instead of replacing the existing PATH value. Replacing it may prevent other applications and operating-system tools from working correctly.

8. Installing the JDK on macOS

On macOS, the JDK is commonly installed using a vendor-provided package installer or another supported installation method.

Installation Steps

  1. Visit the official download page for the chosen JDK distribution.
  2. Select the macOS package compatible with your processor architecture.
  3. Download the installer, commonly provided as a .dmg or .pkg file.
  4. Open the downloaded file and follow the installation instructions.
  5. Complete the installation and open Terminal.
  6. Run java -version to inspect the Java version selected by your system.
  7. Run javac -version to verify compiler availability.

Checking the JDK Location

On many macOS installations, this command lists detected JDK installations:

/usr/libexec/java_home -V

To print the path of the JDK selected by the system, use:

/usr/libexec/java_home

For a terminal session, you can set JAVA_HOME using the following command:

export JAVA_HOME=$(/usr/libexec/java_home)
export PATH="$JAVA_HOME/bin:$PATH"

These commands affect the current shell session unless you add the configuration to the appropriate shell startup file. The exact configuration may differ according to the shell and JDK distribution.

9. Installing the JDK on Linux

Linux users can install a JDK through their distribution's package manager or by downloading an archive or package from a JDK vendor. Package names and commands vary by Linux distribution and repository.

Example: Ubuntu or Debian-Based Systems

Refresh package information:

sudo apt update

Install a JDK package available in the configured repositories. For example, on a repository that provides OpenJDK 21:

sudo apt install openjdk-21-jdk

If that package is unavailable, check the versions supported by your distribution and repositories. Choose a package that matches your course or project requirements.

Example: Fedora-Based Systems

On Fedora systems with the relevant package available, a JDK can be installed with a command similar to:

sudo dnf install java-21-openjdk-devel

Package names and available versions can differ between distributions and releases.

Verify the Linux Installation

java -version
javac -version

Setting JAVA_HOME on Linux

First, identify the actual JDK installation directory. You can inspect the resolved location of the Java executable with:

readlink -f "$(command -v java)"

The resulting path may point to a file inside the JDK's bin directory or to a system-managed alternative. Determine the correct JDK root directory before assigning JAVA_HOME.

For a temporary shell configuration, use the appropriate path:

export JAVA_HOME=/path/to/your/jdk
export PATH="$JAVA_HOME/bin:$PATH"

Replace /path/to/your/jdk with the actual JDK root directory. For persistent configuration, add the correct commands to the relevant shell configuration file or use your operating system's supported environment configuration method.

10. Verifying the JDK Installation

After installing and configuring the JDK, verify that the operating system can locate both the Java runtime command and the Java compiler.

Check the Java Runtime Version

Open a new Command Prompt, PowerShell, Terminal, or Linux shell and run:

java -version

The output should display the installed or selected Java runtime version. The exact output depends on the vendor and version.

Check the Java Compiler Version

javac -version

This command should display the compiler version. If the command is not recognized, the JDK may not be installed correctly or its bin directory may not be available through PATH.

Check JAVA_HOME

On Windows Command Prompt:

echo %JAVA_HOME%

On Windows PowerShell:

$env:JAVA_HOME

On macOS or Linux shells:

echo "$JAVA_HOME"

The displayed value should point to the JDK root directory, not to a source file or the bin folder.

11. Writing and Running Your First Java Program

After confirming that the JDK works, create a simple Java program to test the complete development workflow.

Step 1: Create a Java Source File

Create a file named HelloJava.java and enter the following code:

public class HelloJava {
    public static void main(String[] args) {
        System.out.println("Java JDK is configured successfully!");
    }
}

Step 2: Open the Terminal in the File's Directory

Use Command Prompt, PowerShell, or a terminal and navigate to the folder containing HelloJava.java.

Step 3: Compile the Program

javac HelloJava.java

If compilation succeeds, the compiler creates a file named HelloJava.class in the output directory.

Step 4: Run the Program

java HelloJava

Expected output:

Java JDK is configured successfully!

When running the program, use the class name without the .java or .class extension. This simple exercise confirms that source code can be compiled and executed using the JDK.

12. Understanding Important JDK Directories and Tools

A JDK installation contains multiple directories and tools. Their exact arrangement may vary by version and distribution.

Tool or Directory Purpose
bin Contains executable development and runtime tools.
javac Compiles Java source code into bytecode.
java Launches Java applications.
jshell Provides an interactive environment for experimenting with Java expressions and statements in supported JDK versions.
javadoc Generates API documentation from source comments.
jar Creates and manages Java archive files.
lib Contains libraries and supporting files used by the JDK.

13. Configuring Java in an IDE

An Integrated Development Environment (IDE) makes it easier to write, compile, run, and debug Java applications. Examples include IntelliJ IDEA, Eclipse, and Visual Studio Code with appropriate Java extensions.

General IDE Configuration Steps

  1. Install a JDK that is compatible with the project.
  2. Install your preferred IDE and any required Java extensions or plugins.
  3. Open the IDE's Java or SDK configuration settings.
  4. Select the installed JDK directory or configure the required Java runtime.
  5. Set the project language level or Java version to a compatible value.
  6. Create or open a Java project.
  7. Run a simple Java class and confirm that it executes successfully.

Important: Installing an IDE does not always install a separate system-wide JDK or configure PATH and JAVA_HOME for every application. Verify the IDE's selected runtime and the terminal configuration independently.

14. Common JDK Installation and Configuration Errors

Error or Symptom Possible Cause Solution
java is not recognized on Windows The JDK's bin directory is missing from PATH, or the terminal has not refreshed its environment. Check PATH, verify the installation directory, and open a new terminal.
javac is not recognized The JDK is missing, or PATH points to the wrong location. Confirm that the JDK contains javac and add the correct bin directory to PATH.
JAVA_HOME points to an invalid directory The path contains a typo or points to the bin folder instead of the JDK root. Set JAVA_HOME to the actual JDK root directory.
An unexpected Java version appears Multiple JDK installations or system-managed alternatives may be present. Inspect PATH order, JAVA_HOME, and the executable selected by the system.
Compilation fails with a version error The source code or compiled class may require a different Java version. Check the project's required Java version and the compiler selected by the terminal or IDE.
Permission denied during installation The selected directory requires additional privileges. Use an authorized installation location or obtain the required permissions.
The installer is incompatible The package may not match the operating system or processor architecture. Download the appropriate package from the vendor's official site.

15. Troubleshooting Commands

These commands help identify which Java executable is selected by the system.

Windows Command Prompt

where java
where javac
echo %JAVA_HOME%

Windows PowerShell

Get-Command java
Get-Command javac
$env:JAVA_HOME

macOS and Linux

which java
which javac
echo "$JAVA_HOME"

On Unix-like systems, which shows the command found through PATH. On macOS, you can also use /usr/libexec/java_home -V to list detected JDK installations when the utility is available.

If multiple versions are installed, check the selected executable and update the configuration carefully. Avoid deleting existing Java installations until you know which applications depend on them.

16. Best Practices for JDK Configuration

  • Download the JDK from a trusted vendor or official distribution website.
  • Choose a version compatible with your learning materials and project requirements.
  • Set JAVA_HOME to the JDK root directory when your tools require it.
  • Add the JDK's bin directory to PATH if you want to run Java commands from a terminal.
  • Keep a record of installed JDK versions when working with multiple projects.
  • Check the Java runtime and compiler versions before troubleshooting application issues.
  • Restart the terminal after changing environment variables.
  • Configure the IDE's JDK and project language level consistently with the project requirements.
  • Review licence terms and update policies for the JDK distribution you use.
  • Avoid overwriting the existing PATH variable or copying example installation paths without checking your system.

17. Practical Exercises

Exercise 1: Install a JDK

Download and install a suitable JDK version for your operating system. Record the version number and installation directory.

Exercise 2: Configure Environment Variables

Set JAVA_HOME to your JDK root directory and configure PATH to include the JDK's bin directory. Open a new terminal and verify the values.

Exercise 3: Verify Java Tools

Run java -version and javac -version. Write down the output and explain what each command verifies.

Exercise 4: Compile and Run a Program

Create HelloJava.java, compile it with javac, and execute it with java. Confirm that the expected message appears.

Exercise 5: Troubleshoot a PATH Problem

Describe the steps you would take if java works but javac is not recognized. Include checks for the installation directory, PATH, and the selected JDK.

18. Mini Project: Create a Java Setup Verification Program

Create a small Java application that displays a welcome message and the current Java runtime version.

public class JavaSetupCheck {
    public static void main(String[] args) {
        System.out.println("Java environment check");
        System.out.println("Java version: " +
            System.getProperty("java.version"));
        System.out.println("Operating system: " +
            System.getProperty("os.name"));
    }
}

Save the file as JavaSetupCheck.java, then compile and run it:

javac JavaSetupCheck.java
java JavaSetupCheck

This project demonstrates that a Java application can retrieve runtime information through system properties. Remember that the program reports the runtime executing it; use javac -version separately to inspect the compiler version.

19. Interview Questions and Answers

Q1. What is the JDK?

Answer: The Java Development Kit is a collection of development tools and libraries used to compile, debug, and run Java applications.

Q2. What is the difference between JDK and JVM?

Answer: The JDK provides development tools and supporting components, while the JVM executes Java bytecode at runtime.

Q3. What is JAVA_HOME?

Answer: JAVA_HOME is an environment variable commonly used to identify the root directory of a JDK installation.

Q4. Why is the JDK bin directory added to PATH?

Answer: It allows commands such as java and javac to be found and executed from a terminal without specifying their full paths.

Q5. What does javac do?

Answer: The Java compiler translates Java source files into class files containing Java bytecode.

Q6. How do you check whether the JDK is installed?

Answer: Run javac -version and java -version. The compiler command is particularly useful for confirming that development tools are available.

Q7. Why might multiple Java versions cause problems?

Answer: The terminal, IDE, or build tool may select a different Java version from the one expected by the project. Check PATH, JAVA_HOME, and each tool's configured JDK.

Q8. Is JAVA_HOME always required?

Answer: No. Basic Java commands can work through PATH without JAVA_HOME, but many build tools and development environments use it to locate a JDK.

Q9. Can Java be developed without an IDE?

Answer: Yes. Java programs can be written in a text editor and compiled and executed from a terminal using the JDK tools.

Q10. What should you do after changing PATH?

Answer: Open a new terminal session and verify the commands again so that the updated environment variables are loaded.

20. Quick Revision Checklist

  • Understand the purpose of the JDK, JRE, and JVM.
  • Choose a compatible JDK version and download it from a trusted source.
  • Install the JDK for your operating system.
  • Identify the actual JDK installation directory.
  • Configure JAVA_HOME if required by your development tools.
  • Add the JDK bin directory to PATH when needed.
  • Verify the runtime using java -version.
  • Verify the compiler using javac -version.
  • Compile a Java source file and run the resulting class.
  • Understand common PATH, JAVA_HOME, and version-selection problems.

21. Conclusion

Installing and configuring the Java Development Kit is a foundational step in learning Core Java. A correct installation provides the compiler, runtime tools, and libraries needed to develop Java applications.

By selecting a compatible JDK, configuring JAVA_HOME and PATH when needed, checking the Java and compiler versions, and compiling a simple program, learners can establish a reliable Java development environment. These skills are useful when working with command-line tools, IDEs, and Java projects.

To explore structured Core Java learning resources, visit JustAcademy's Core Java Training Course, register for a course demo, or visit the JustAcademy official website.

whatsapp